from numpy import zeros

def conv(dlist,ref=5.0):
    dnew = zeros(len(dlist)/2)
    t = 0
    for i in range(len(dnew)):
        #t += ((dlist[2*i] & 0xfc) >> 2) * (64.0 / 16e6)
        #dnew[i,0] = t
        dnew[i] = (((dlist[2*i] & 0x03) << 8) | dlist[2*i +1])/1024.0 *ref
    return dnew

def acquire(port,time=10.0,res=1e-4):
    '''
    Module for acquiring data from xoDAQ.
    >>> data = q.acquire(port,time=1)
    '''
    data = []
    nbytes = 2*(time/res)
    port.flushInput()
    port.wrby(0x01) #Start Acquiring
    while(1):
        data.append(port.reby())
        if(len(data) == nbytes):
            port.wrby(0x02) #Stop acquiring
            break
    port.flushInput()
    return conv(data)
